package com.eascs.spider.dsk.entity;

import java.math.BigDecimal;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.json.JSONObject;

/**
 * 鼎搜客翻页查询条件parameterdata实体json
 * @author Administrator
 *
 */
public class QueryForm {

	private String[] brands = {"休闲食品(20937)","饮料(11360)","冲调食品(2302)","酒类(11556)",
							   "调味品(3988)","乳制品(3931)","肉制品(852)","方便食品(4750)",
							   "农副产品(2611)","低温食品(2354)","日化用品(1018)","纸品行业(210)",
							   "医药行业(324)"};
	
	private String[] channels = {"商超渠道(12931)","流通渠道(20650)","餐饮渠道(3933)","教育渠道(411)",
								 "旅游渠道(210)","夜场渠道(145)","婚庆渠道(1231)","药店渠道(678)",
								 "电商渠道(229)","外贸渠道(1659)","礼品团购(574)","其他渠道(844)"};
	
	private static Queue<String> cbs = new ConcurrentLinkedQueue<>();
	
	private static QueryForm instance = null;
	
	public static synchronized QueryForm getInstance(){
		return instance == null? instance = new QueryForm(): instance;
	}
	
	private QueryForm(){
		initCB();
	}
	
	public JSONObject getQueryFormJson(){
		JSONObject json = new JSONObject();
		json.put("fUser_Id", getTel());
		String cb = cbs.poll();
		if(cb == null)
			return null;
		json.put("Brand", cb.split("\\|")[0]);
		json.put("Channel", cb.split("\\|")[1]);
		String gps = getBeijingll();
		json.put("gpsX", gps.split("\\|")[0]);
		json.put("gpsY", gps.split("\\|")[1]);
		json.put("PageIndex", "1");
		json.put("Sort", "-1");
		json.put("CityId", "");
		json.put("FindString", "");
		json.put("CityId", "");
		return json;
	}
	
	public JSONObject getQueryFormJsonModel(){
		JSONObject json = new JSONObject();
		json.put("fUser_Id", getTel());
		json.put("Brand", "");
		json.put("Channel", "");
		String gps = getBeijingll();
		json.put("gpsX", gps.split("\\|")[0]);
		json.put("gpsY", gps.split("\\|")[1]);
		json.put("PageIndex", "1");
		json.put("Sort", "-1");
		json.put("CityId", "");
		json.put("FindString", "");
		json.put("CityId", "");
		return json;
	}	
	
	/**
	 * 初始化渠道及行业组合
	 * @return
	 */
	private void initCB(){
		for(String brand : brands){
			for(String channel : channels){
				cbs.add(brand + "|" + channel);
			}
		}
	}
	
	/**
	 * 随机获取手机号
	 * @return
	 */
    public String getTel() {
        int index=getNum(0,telFirst.length-1);
        String first=telFirst[index];
        String second=String.valueOf(getNum(1,888)+10000).substring(1);
        String third=String.valueOf(getNum(1,9100)+10000).substring(1);
        return first+second+third;
    }
    
    /**
     * 返回手机号码 
     */
    private String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
    
    private int getNum(int start,int end) {
        return (int)(Math.random()*(end-start+1)+start);
    }
    
    //随机取个北京的经纬度
   	private String getBeijingll(){
   		return randomLonLat(30, 40, 116, 117);
   	}
   	
   	/**
    * @Title: randomLonLat
    * @Description: 在矩形内随机生成经纬度
    * @param MinLon：最小经度  MaxLon： 最大经度   MinLat：最小纬度   MaxLat：最大纬度    type：设置返回经度还是纬度
    * @return
    * @throws
    */
   	private String randomLonLat(double MinLon, double MaxLon, double MinLat, double MaxLat) {
   		BigDecimal db = new BigDecimal(Math.random() * (MaxLon - MinLon) + MinLon);
   		String lon = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString();// 小数后6位
   		db = new BigDecimal(Math.random() * (MaxLat - MinLat) + MinLat);
   		String lat = db.setScale(6, BigDecimal.ROUND_HALF_UP).toString();
   		return lon +"|"+lat;
  }

   	public static void main(String[] args) {
		QueryForm qr = QueryForm.getInstance();
		while(true){
			JSONObject param = qr.getQueryFormJson();
			if(param == null)
				break;
			System.out.println(param.toString());
		}
	}
}
